package com.fz.controller;

import com.fz.entity.Information;
import com.fz.entity.Messageadd;
import com.fz.mapper.WebMessageMapper;
import com.fz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Z-C-Y on 2018-03-07.
 * message.jsp=====资讯
 */
@Controller
public class WebMessageController {
    @Autowired
    protected WebMessageMapper webMessageMapper;

    @RequestMapping("/webpage/message")
    public String message(Model model, HttpServletRequest req) {
        /*页面字段拿出来*/
        List<Messageadd> listMessageadd = webMessageMapper.quryMessageadd();
        model.addAttribute("listMessageadd",listMessageadd);
        /*分页*/
        /*实现分页并查询的功能*/
        int  pageNun1 = 1;
        if((req.getParameter("pageNun") != null) && !("".equals(req.getParameter("pageNun")))){
            pageNun1 = Integer.parseInt(req.getParameter("pageNun"));
        }
        //System.out.println("==获得当前是第几页==pageNun===="+req.getParameter("pageNun"));
        //每页显示的记录数
        int pageSize = 3;
        /*分页======查询数据库的数据的总数据*/
        List<Information> list4 = webMessageMapper.queryInformation();
        model.addAttribute("list4Product",list4);

        /*得到表中总条数*/
        int totalRecord = list4.size();

        //有了初始化数据，就可以创建PageBean对象了
        PageBean pbm = new PageBean(pageNun1, pageSize, totalRecord);

        model.addAttribute("pbm",pbm);
        //获取PageBeanManage对象中的startIndex;
        int startIndex = pbm.getStartIndex();

        //有了pageSize和startIndex，就可以拿到每页的数据
        List<Information> list5 =  webMessageMapper.queryInformationData(startIndex,pageSize);
        model.addAttribute("list5",list5);
        model.addAttribute("t","message");

        return "/webpage/message";
    }

}
